﻿#region

using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using KenLib.BusinessManager;

#endregion

public partial class Org_frmO_TraineeList : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["役"] != null)
        {
            ddl_Type.Focus();
            ((Org_MasterPage) Master).changeTitle("技能実習生一覧");
            ((Org_MasterPage) Master).HideLiArea("frmO_TraineeList");
            Title = "技能実習生一覧";
            grv_human.PageSize = AppCode_Common.getPageSize();
            //AppCode_Common.ShowEmtyGrid<年数回の賞与>(grv_human,7, "データーがあります。", "");  
            frm_TraineeListManager l_frmTraineeListManager = new frm_TraineeListManager();
            if (!IsPostBack)
            {
                load_ddl_Type();

                if (Session["dtt_OR00003F"] != null)
                {
                    loadSessionToForm();
                }
                else
                {
                    TypeChange();
                    GroupChange();
                    CompanyChange();
                    ddl_Type.Focus();
                    btn_Search_Click(null, null);
                }
            }
        }
        else
        {
            Response.Redirect("~/frmD_Login.aspx");
        }
    }

    /// <summary>
    /// Loads the group.
    /// </summary>
    /// <param name="idType">Type of the id.</param>
    /// <returns></returns>
    private static DataTable loadGroup(string idType)
    {
        frm_TraineeListManager l_frmTraineeListManager = new frm_TraineeListManager();
        DataTable Result = l_frmTraineeListManager.GetAllGroup(idType);
        return Result;
    }

    /// <summary>
    /// Loads the company.
    /// </summary>
    /// <param name="idGroup">The id group.</param>
    /// <returns></returns>
    private static DataTable loadCompany(string idGroup)
    {
        frm_TraineeListManager l_frmTraineeListManager = new frm_TraineeListManager();
        DataTable Result = l_frmTraineeListManager.GetCommanyByGroup(idGroup);
        return Result;
    }

    /// <summary>
    /// Loads the sub COM.
    /// </summary>
    /// <param name="idGroup">The id group.</param>
    /// <param name="idCom">The id COM.</param>
    /// <returns></returns>
    private static DataTable loadSubCom(string idGroup, string idCom)
    {
        frm_TraineeListManager l_frmTraineeListManager = new frm_TraineeListManager();
        DataTable Result = l_frmTraineeListManager.GetSubComByComany(idGroup, idCom);
        return Result;
    }

    protected void btn_Search_Click(object sender, EventArgs e)
    {
        ((Org_MasterPage) Master).changeTitle("技能実習生一覧");
        Title = "技能実習生一覧";
        string idGroup = ddl_Group.SelectedValue;
        string idCompany = ddl_Company.SelectedValue;
        string idSubCom = ddl_SubCom.SelectedValue;
        string Search = txt_Search.Text;

        if (ddl_Group.Items.Count < 2)
        {
            idGroup = "-2";
        }

        frm_TraineeListManager l_frmTraineeListManager = new frm_TraineeListManager();
        DataTable Result = l_frmTraineeListManager.Search(ddl_Type.SelectedValue, idGroup, idCompany, idSubCom,
                                                          Search.Replace("'", "''"));
        grv_human.DataSource = Result;
        grv_human.DataBind();
        DtTableToSession(Result);
        Control lbl = Panel1.FindControl("div_lbl");
        if (Result.Rows.Count > 0)
        {
            grv_human.DataSource = Result;
            grv_human.DataBind();
            lbl.Visible = true;
        }
        else
        {
            ShowEmtyGrid(grv_human, 4, "データがありません。", "");
            lbl.Visible = false;
        }
        ddl_Type.Focus();
        if (sender != null)
        {
            regisEvent();
        }
    }

    /// <summary>
    /// Dts the table to session.
    /// </summary>
    /// <param name="dtt">The DTT.</param>
    private void DtTableToSession(DataTable dtt)
    {
        Session["dtt_OR00003F"] = dtt;
        Session["ddl1_OR00003F"] = ddl_Type.SelectedValue;
        Session["ddl2_OR00003F"] = ddl_Group.SelectedValue;
        Session["ddl3_OR00003F"] = ddl_Company.SelectedValue;
        Session["ddl4_OR00003F"] = ddl_SubCom.SelectedValue;
        Session["txt_OR00003F"] = txt_Search.Text;
    }

    /// <summary>
    /// Loads the session to form.
    /// </summary>
    private void loadSessionToForm()
    {
        if (Session["dtt_OR00003F"] != null)
        {
            DataTable dtt = (DataTable) Session["dtt_OR00003F"];
            Control lbl = Panel1.FindControl("div_lbl");
            if (dtt.Rows.Count > 0)
            {
                grv_human.DataSource = dtt;
                grv_human.DataBind();
                lbl.Visible = true;
            }
            else
            {
                ShowEmtyGrid(grv_human, 4, "データがありません。", "");
                lbl.Visible = false;
            }
        }


        if (Session["ddl1_OR00003F"] != null)
        {
            ddl_Type.SelectedValue = Session["ddl1_OR00003F"].ToString();
        }

        TypeChange();
        if (Session["ddl2_OR00003F"] != null)
        {
            ddl_Group.SelectedValue = Session["ddl2_OR00003F"].ToString();
        }
        GroupChange();
        if (Session["ddl3_OR00003F"] != null)
        {
            ddl_Company.SelectedValue = Session["ddl3_OR00003F"].ToString();
        }
        CompanyChange();
        if (Session["ddl4_OR00003F"] != null)
        {
            ddl_SubCom.SelectedValue = Session["ddl4_OR00003F"].ToString();
        }
        if (Session["txt_OR00003F"] != null)
        {
            txt_Search.Text = Session["txt_OR00003F"].ToString();
        }
    }

    /// <summary>
    /// Get2号移行年月日s the specified _date.
    /// </summary>
    /// <param name="_date">The _date.</param>
    /// <returns></returns>
    protected String get2号移行年月日(object _date)
    {
        try
        {
            DateTime date = (DateTime) _date;
            return new DateTime(date.Year + 1, date.Month, date.Day).ToString("yyyy年MM月dd日");
        }
        catch
        {
            return "";
        }
    }

    /// <summary>
    /// Get性別s the specified _date.
    /// </summary>
    /// <param name="_date">The _date.</param>
    /// <returns></returns>
    protected String get性別(object _date)
    {
        try
        {
            bool date = (bool) _date;
            return date ? "女" : "男";
        }
        catch
        {
            return "";
        }
    }

    /// <summary>
    /// Shows the emty grid.
    /// </summary>
    /// <param name="grv">The GRV.</param>
    /// <param name="iNumberColumn">The i number column.</param>
    /// <param name="strTextShow">The STR text show.</param>
    /// <param name="strCssClass">The STR CSS class.</param>
    private void ShowEmtyGrid(GridView grv, int iNumberColumn, string strTextShow, string strCssClass)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("社員ID");
        dt.Columns.Add("氏名3");
        dt.Columns.Add("性別");
        dt.Columns.Add("生年月日");
        dt.Columns.Add("国籍名");
        dt.Columns.Add("入国年月日");
        dt.Columns.Add("企業名");
        dt.Columns.Add("");
        DataRow dtr = dt.NewRow();
        dt.Rows.Add(dtr);
        grv.ShowFooter = false;
        grv.DataSource = dt;
        grv.DataBind();

        //iNumberColumn = grv.Columns.Count;
        TableCell cell = new TableCell();
        cell.ColumnSpan = 8;
        cell.Text = strTextShow;
        if (!string.IsNullOrEmpty(strCssClass))
            cell.CssClass = strCssClass;
        grv.Rows[0].Cells.Clear();
        grv.Rows[0].Cells.Add(cell);
    }

    protected void grv_human_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grv_human.PageIndex = e.NewPageIndex;
        btn_Search_Click(btn_Search, new EventArgs());
        regisEvent();
    }

    /// <summary>
    /// Regises the event.
    /// </summary>
    private void regisEvent()
    {
        string script = "" +
                        "disableControlOnLoadUpdatePanel(\"" + up.ClientID + "\",\"" + ddl_Type.ClientID + "," +
                        ddl_Group.ClientID + "," + ddl_Company.ClientID + "," + ddl_SubCom.ClientID + "\");" +
                        "registerEnterEvent('enter', '" + btn_Search.ClientID + "', 5);";
        ScriptManager.RegisterStartupScript(up, up.GetType(), "error", script, true);
    }

    /// <summary>
    /// Load_ddl_s the type.
    /// </summary>
    private void load_ddl_Type()
    {
        ddl_Type.Items.Add(new ListItem("在籍", "1"));
        ddl_Type.Items.Add(new ListItem("帰国", "2"));
        ddl_Type.Items.Add(new ListItem("全", "3"));
    }

    protected void ddl_Type_SelectedIndexChanged(object sender, EventArgs e)
    {
        TypeChange();
        GroupChange();
        CompanyChange();
        btn_Search_Click(null, null);
        ddl_Group.Focus();
        regisEvent();
    }

    protected void ddl_Group_SelectedIndexChanged(object sender, EventArgs e)
    {
        GroupChange();
        CompanyChange();
        btn_Search_Click(null, null);
        ddl_Company.Focus();
        regisEvent();
    }

    protected void ddl_Company_SelectedIndexChanged(object sender, EventArgs e)
    {
        CompanyChange();
        btn_Search_Click(null, null);
        ddl_SubCom.Focus();
        regisEvent();
    }

    /// <summary>
    /// Types the change.
    /// </summary>
    private void TypeChange()
    {
        ddl_Group.Items.Clear();
        ddl_Group.Items.Add(new ListItem("[選択]", "-1"));
        foreach (DataRow row in loadGroup(ddl_Type.SelectedValue).Rows)
        {
            ddl_Group.Items.Add(new ListItem(row["入国Ｇ名"].ToString(), row["ID"].ToString()));
        }
    }

    /// <summary>
    /// Groups the change.
    /// </summary>
    private void GroupChange()
    {
        ddl_Company.Items.Clear();
        ddl_Company.Items.Add(new ListItem("[選択]", "-1"));
        foreach (DataRow row in loadCompany(ddl_Group.SelectedValue).Rows)
        {
            ddl_Company.Items.Add(new ListItem(row["企業名"].ToString(), row["企業ID"].ToString()));
        }
    }

    /// <summary>
    /// Companies the change.
    /// </summary>
    private void CompanyChange()
    {
        ddl_SubCom.Items.Clear();
        ddl_SubCom.Items.Add(new ListItem("[選択]", "-1"));
        foreach (DataRow row in loadSubCom(ddl_Group.SelectedValue, ddl_Company.SelectedValue).Rows)
        {
            ddl_SubCom.Items.Add(new ListItem(row["拠点名"].ToString(), row["拠点ID"].ToString()));
        }
    }

    protected void ddl_SubCom_SelectedIndexChanged(object sender, EventArgs e)
    {
        btn_Search_Click(null, null);
        string script = "" +
                        "disableControlOnLoadUpdatePanel(\"" + up.ClientID + "\",\"" + ddl_Type.ClientID + "," +
                        ddl_Group.ClientID + "," + ddl_Company.ClientID + "," + ddl_SubCom.ClientID + "\");" +
                        "registerEnterEvent('enter', '" + btn_Search.ClientID + "', 5);setTimeout(function() { $('#" +
                        txt_Search.ClientID + "').focus() },100);";
        ScriptManager.RegisterStartupScript(up, up.GetType(), "error", script, true);
    }
}